<?php
require_once 'login.php';
echo <<<_END

<!DOCTYPE html>
<!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>         <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js map-page"> <!--<![endif]-->

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>Photogrammar: Advanced Search Results</title>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <link rel="shortcut icon" href="http://cartodb.com/assets/favicon.ico" />
    <link rel="stylesheet" type="text/css" href="/css/styleMain.css"/>
    <link rel="stylesheet" href="/css/leaflet.css" />
    <!--[if lte IE 8]><link rel="stylesheet" href="http://code.leafletjs.com/leaflet-0.3.1/leaflet.ie.css" /><![endif]-->
    <link  href="/css/cartodb-leaflet.css" rel="stylesheet" type="text/css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script src="/js/leaflet.js"></script>
    <script type="text/javascript" src="/js/wax.leaf.min-6.2.3-touched.js"></script>
    <script type="text/javascript" src="/dist/cartodb-leaflet-min.js"></script>
    <script type="text/javascript" src="/dist/cartodb-popup-min.js"></script>
    
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <script type="text/javascript" src="/js/jquery.google_menu.js"></script>
    
    <script>
            $('document').ready(function(){
                $('.menu').fixedMenu();
            });
    </script>
    
    <script type="text/javascript" src="../js/mapping.js"></script>
</head>
<body onload="initialize()">

<div id="wrapper" class="clearfix">

<div id="header" class="clearfix">
	<div id="logo">
    	<h1><a href="/"><img src="/images/photogrammar-wordmark.jpg" alt="Yale Photogrammar" title="Yale Photogrammar" /></a></h1>
    </div><!-- /#logo -->
    <nav id="menu" class="menu">
        <ul>
          <li class="single-link">
            <a href="/index.php">Home</a>
            </li>
          <li class="single-link">
            <a href="/map/">Map</a>
            </li>
          <li class="single-link"><!-- Using class="single-link" for links with no dropdown -->
            <a href="/browse/">Browse</a>
            </li>
          <li class="current"><!-- Using class="single-link" for links with no dropdown -->
            <a href="/search/">Search</a>
            </li>
          <li class="single-link"><!-- Using class="single-link" for links with no dropdown -->
            <a href="/about/">About</a>
            </li>
          <li class="single-link"><!-- Using class="single-link" for links with no dropdown -->
            <a href="/blog/">Blog</a>
            </li>
        </ul>
    </nav><!-- /#menu -->
</div><!-- /#header -->

<div id="content-wrapper" class="clearfix">

<div id="results-content" class="clearfix">

_END;

$db_server = mysql_connect($db_hostname, $db_username, $db_password);

if(!$db_server) die("Unable to connect to MySQL: " .mysql_error());

mysql_select_db($db_database) or die('Unable to connect to MySQL: ' . mysql_error());




    $query = "SELECT * FROM photo3";


    // Query mysql:
    $result = mysql_query($query);

    if (!$result) die ("Database access failed: " . mysql_error());

    $rows = mysql_num_rows($result);
    $j = get_post('qnum');
    $ans = get_post('ans');
    $user_id = get_post('user_id');
    if($j == '') $j = 0;

    if($user_id == '') {
        $user_id = generateRandomString(15);
        echo "<h2> Welcome to tag that... <a href='http://photogrammar.yale.edu/testing/index.php?ans=&qnum=0&user_id=". $user_id . "'> click here to enter</a></h2>";
    } else {
        if($j != 0) {
            $query2 = "INSERT INTO photo4(userid, photonum, answer,time) VALUES('" . $user_id . "','" . $j . "','" . $ans .   "',NOW())";
            mysql_query($query2);
        }

        if($j < $rows) {

            $ptitle = mysql_result($result, $j, 'title');
            $purl = '<img width=400 src="http://maps.library.yale.edu/images/public/photogrammar/' . mysql_result($result, $j, 'large_url') . '" />';    
            $pop1 = mysql_result($result, $j, 'pop1');
            $pop2 = mysql_result($result, $j, 'pop2');
            $pop3 = mysql_result($result, $j, 'pop3');

            echo $purl . '</br>';

            echo "<form action='index.php' method='get'>";

            echo "<h3>";
            echo '<input type="radio" name="ans" value="' . $pop1 . '">&nbsp' . $pop1 .'<br>';
            echo '<input type="radio" name="ans" value="' . $pop2 . '">&nbsp' . $pop2 .'<br>';
            echo '<input type="radio" name="ans" value="' . $pop3 . '">&nbsp' . $pop3 .'<br>';
            echo "</h3>";

            echo '</br>';
            echo '<input type="hidden" name="qnum" id="orderNumber" value="' . ($j + 1) . '" />';
            echo '<input type="hidden" name="user_id" id="user_id" value="' . $user_id . '" />';

            echo "<div id='search-button'>";
	        echo "<button type='submit' value='Search'/>Next photo</button>";
            echo "</div><!--/#search-button-->";
            echo "</form><br>";
        } else {
            echo "<h2> Thank you! </h2> Your responses:<br><br>";
            $query3 = "SELECT * FROM photo4 WHERE userid='" . $user_id . "'";
            $result3 = mysql_query($query3);
            $rows3 = mysql_num_rows($result3);
            for($k = 0; $k < $rows3; ++$k)
            {
                echo 'Photo ' . mysql_result($result3, $k, 'photonum') . ': ' . mysql_result($result3, $k, 'answer');
                echo '<br>';
            }

            echo "<h2> Thank you! </h2> Other's responses:<br><br>";
            $query4 = "select photonum,answer,count(answer) AS cnt from photo4 group by photonum,answer";
            $result4 = mysql_query($query4);
            $rows4 = mysql_num_rows($result4);
            for($k = 0; $k < $rows4; ++$k)
            {
                echo 'Photo ' . mysql_result($result4, $k, 'photonum') . ': ' . mysql_result($result4, $k, 'answer') . ' (' . mysql_result($result4, $k, 'cnt') . ')';
                echo '<br>';
            }

        }
    }
mysql_close($db_server);

function get_post($var)
{
    return mysql_real_escape_string($_GET[$var]);
}

function generateRandomString($length) {    
    return substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, $length);
}


echo <<<_END

</div><!--/#results-content-->

</div><!--/#content-wrapper-->

</div><!--/#wrapper-->

</body>

</html>
_END;

?>
